#https://github.com/ntasfi/PyGame-Learning-Environment
#MUAI:基础AI
#https://github.com/datamllab/rlcard
#HOAI:卡牌游戏AI

#####todo：设计基础游戏框架
#1.design the card :

import random

#from getkey import getkey
# pip install keyboard
import keyboard

import time
#卡片类
class Card:
    def __init__(self,type,type_lable,color,label,label_value,theme=1):
        self.type=type
        self.type_lable=type_lable
        self.color=color
        self.lable=label
        self.lable_value=label_value
        self.theme=theme
    type:int
    color:int
    lable:str
    theme:int
    lable_value:int
    def get_card(self):
            return  self.type_lable+self.lable
   
        

#选手类
class Player:
    def __init__(self,name,level,human=0):
        self.name=name
        self.level=level
        self.cards=[]
        self.pos_index=0
        self.human=0
    human:int
    name:str
    level:int
    cards:list
    pos_index:int=0
    selected_list:set=[]
    
    

    def culc_pos_index(self,pos):
        if(pos==1):
            #如果左移
            if(self.pos_index<1):
                self.pos_index=len(self.cards)-1
            else:
                self.pos_index=self.pos_index-1
        elif(pos==2):
            #如果右移
            if(self.pos_index>len(self.cards)-1):
                self.pos_index=0
            else:
                self.pos_index=self.pos_index+1
        elif(pos==3):
            #如果上移
            self.selected_list.append(self.pos_index)
        elif(pos==4):
            #如果下移
            self.selected_list.remove(self.pos_index)
        else:
            print("fault")
        
        self.show_cards_it()
                
    


    def add_card(self,card):
        self.cards.append(card)
    
    def show_cards(self):
        cards_str=" "
        for card in self.cards:
            cards_str+=str(card.get_card())+" "
        print (cards_str)

    #交互显示卡片
    def show_cards_it(self):
        
        #正常输出
        cards_str=" "
        for i in range(len(self.cards)):
            if(i==self.pos_index):
                cards_str+="\033[33;43m"+str(self.cards[i].get_card())+"\033[0m"+" "
            else:
                if(self.selected_list.__contains__(i)):
                    cards_str+="\033[33;44m"+str(self.cards[i].get_card())+"\033[0m"+" "
                else:
                    cards_str+=str(self.cards[i].get_card())+" "
        print (cards_str,end="")



    def sort_cards(self):
        self.cards=sorted(self.cards,key=lambda card:card.lable_value)

class AdvPlayer(Player):
    def __init__(self,splayer:Player,nplayer:Player,lplayer:Player):
        self.self_player = splayer
        self.next_player =nplayer
        self.last_player =lplayer
    self_player:Player
    next_player:Player
    last_player:Player
    


#初始化总牌组
huase:list=["♠️","♣️","♥️","♦️"]
biaoqian:list=["3","4","5","6","7","8","9","10","J","Q","K","A","2"]

gm_cards:list=[Card(5,"黑",1,"Jocker",16),Card(6,"彩",0,"Jocker",17)]

ni:int=0
for i in huase:
    nj:int=0
    for j in biaoqian:
        clr=0
        if ni>1: clr=1
        gm_cards.append(Card(ni+1,i,clr,j,nj+3))
        nj+=1
    ni+=1

#初始化选手
nm1:Player=Player("佃农",0)
nm2:Player=Player("樵夫",0)
nm3:Player=Player("长工",0)
nm_players:list=[nm1, nm2, nm3]


#选人提示
n:int =1
for i in nm_players:
    print(str(n)+"."+i.name)
    n+=1

choose_player:int=int(input("选择你最爱的选手："))

#设置玩家选定的选手角色
main_human:Player=nm_players[choose_player-1]
main_human.human=1


#发牌
for i in nm_players:
    for j in range(17):
        tmp=random.choice(gm_cards)
        i.add_card(tmp)
        gm_cards.remove(tmp)


#定义出牌顺序
adv_players:list=[AdvPlayer(nm_players[0],nm_players[1],nm_players[2]),AdvPlayer(nm_players[1],nm_players[2],nm_players[0]),AdvPlayer(nm_players[2],nm_players[0],nm_players[1])]

#选地主
dz:AdvPlayer=random.choice(adv_players)
dz.self_player.level=1


#地主加牌
for i in gm_cards:
    dz.self_player.add_card(i)

#每个选手排序自己的牌
for i in nm_players:
    i.sort_cards()

#提示身份
if (main_human.level==1):
    print("你是地主")
else:
    print("你是"+main_human.name)

#展示牌
print("你的牌是:")
main_human.show_cards()

#当前回合规则
gm_turn_rule=""

gm_is_combo=False


#出牌焦点advPlayer
current_advp:AdvPlayer=dz

#turn结束标志
exit_index=False
#结束回合
def turn_quit():
    global exit_index
    exit_index=True

#出牌
def enter_turn():
    print("出牌")
    #是否匹配规则


    #出牌结束


#是否还原回调标志
is_callback_release = False
#执行回调函数
def callback_invoke(q):
    global is_callback_release
    if(is_callback_release):
        return
    else:
        if(q<5):
            current_advp.self_player.culc_pos_index(q)
        elif(q==5):
            turn_quit()
        else:
            enter_turn()

#把回调还原
def callback_release():
    global is_callback_release
    is_callback_release=True

#注册回调函数
keyboard.on_release_key("a",lambda _:callback_invoke(1))
keyboard.on_release_key("d",lambda _:callback_invoke(2))
keyboard.on_release_key("w",lambda _:callback_invoke(3))
keyboard.on_release_key("s",lambda _:callback_invoke(4))
keyboard.on_release_key("q",lambda _:callback_invoke(5))
keyboard.on_release_key("e",lambda _:callback_invoke(6))

def do_turn():
    current_advp.self_player.culc_pos_index(0)
    print("\r",end="")
    global exit_index,is_callback_release
    exit_index=False
    is_callback_release=False

    while True:
        time.sleep(0.1)
        print("\r",end="")
        if(exit_index):
            is_callback_release=True
            break
        


#地主出牌
if (current_advp.self_player.human==1):
    print("你是地主，请出牌：")
    do_turn()
else:
    print("地主出牌：")
    do_turn()
# while True:
#     print("12")
#     time.sleep(1)

# while True:
#     current_advp=current_advp.next_player
#     do_turn()


#linux上用不了同步监听，感觉这游戏废了
#谁有余力谁开发一下吧，反正有大模型应该没多大难
#算了，用godot吧